/**
 * Created by Cheney on 2017/4/4.
 */
const style = require("./color")

var AU = require("array-util")
var g = global;

try {
    if ( undefined != window){
        g = window
        g.envtype = 'window'
    } else {
        g.envtype = 'global'
    }
} catch (e) {
    g.envtype = 'global'
}



var log4js = function() {
    this.out = console.log;
    this.err = console.error;
    // 控制 log 输出级别
    this.level = (function () {

        if( g.log4js_level ){
            return g.log4js_level
        }
        return 'info'

    })();
    this.module = '';
    this.fileline = true
}

// 存储配置后的 log4js 对象
log4js._container = {};
// 输出级别
log4js.levels = {
    'debug' : 10,
    'info' : 20,
    'error' : 30,
    'close' : 60000
}


log4js.fileline = function () {
    try {
        throw new Error();
    } catch (e) {
        try{
            return e.stack.split(/\n/)[4].match(/\((.+)\)/)[1];
        }catch(e){
            return ""
        }
    }
}


function isString(obj) {
    return typeof(obj) === 'string'
}

function _argsparam(msgs){
    var ap = "";
    for ( let msg of msgs ){
        if ( isString( msg ) ){
            ap += "%s, "
        } else {
            ap += "%O, "
        }
    }
    if ( ap.endsWith(", ") ){
        ap = ap.substr(0, ap.length -2)
    }
    return ap
}

// '%s%s%s', info[0], msg , info[1]
log4js.write = function (out, level, logger, msgs) {
    var module = logger.module
    var param = new AU.Params()
    var color = logger.colors[ level ]

    // 根据环境打印不同的参数
    if ( 'global' === g.envtype ){
        param
            .push('%s%s%s' + _argsparam( msgs ) + '%s' ) // 准备颜色字符串
            .push( color[0] )
            .push( logger.fileline ? log4js.fileline() : '' )
            .push("[" + level + "] " + module + " - ")
            .pushArgs( msgs )
            .push( color[1] )
            .call(out, console)
    }

    if ( 'window' === g.envtype ){
        param
            .push('%c %s' + _argsparam( msgs ) ) // 准备颜色 CSS
            .push('color: ' + logger.colorscss[ level ])
            .push("[" + level + "] " + module + " - ")
            .pushArgs( msgs )
            .call(out, console)
    }


}

log4js.create = function (name, out, err) {
    var logger = new log4js();
    logger.module = name ? name : '';
    if( out ){
        logger.out = out;
        logger.err = out;
    }
    if( err){
        logger.err = err;
    }

    logger.colors = {
        debug : style.cyan,
        info : style.green,
        error : style.red
    }

    logger.colorscss = {
        debug : 'cyan',
        info : 'green',
        error : 'red'
    }

    return logger;
}


log4js.get = function (name, out, err) {
    var logger;
    if( out || err ){
        logger = log4js.create(name, out, err)
        log4js._container[name] = logger;
    }else{
        logger = log4js._container[name];
        if( !logger ){
            logger = log4js.create(name);
            log4js._container[name] = logger;
        }
    }
    return logger;
}





var proto = log4js.prototype

proto.info = function () {
    if( log4js.levels['info'] >= log4js.levels[this.level] ){
        log4js.write(this.out, 'info' , this , arguments)
    }
}

proto.debug = function () {
    if( log4js.levels['debug'] >= log4js.levels[this.level] ){
        log4js.write(this.out, 'debug' , this , arguments)
    }
}

proto.error = function () {
    if( log4js.levels['error'] >= log4js.levels[this.level] ){
        log4js.write(this.out, 'error' , this , arguments)
    }
}

proto.get = log4js.get
proto.i = proto.info
proto.d = proto.debug
proto.e = proto.error




g.log4js = log4js;
g.log = log4js.get()



module.exports = log4js
